/*
 Copyright (C) 2009 QDSS.org
 
 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
 along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.qdss.persist.support;

import java.io.IOException;
import java.util.Properties;

import org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer;

/**
 * 
 * 
 * @author <a href="mailto:devezhao@126.com">FANGFANG ZHAO</a>
 * @since 0.1, 12/04/08
 * @version $Id: SecurityPreferencesConfigurer.java 119 2011-05-15 16:56:18Z zhaofang123@gmail.com $
 */
public class SecurityPreferencesConfigurer extends PreferencesPlaceholderConfigurer {
	
	@Override
	protected void loadProperties(Properties props) throws IOException {
		super.loadProperties(props);
		
		this.afterLoad(props);
	}
	
	/*-
	 */
	protected void afterLoad(Properties props) {
		String dbUrl = props.getProperty("db.url");
		
		if (dbUrl.startsWith("jdbc:mysql://")) {
			String append = "";
			// Fix MySQL Connector/J 3.1 or higher Bugs
			if (!dbUrl.contains("zeroDateTimeBehavior")) {
				append = "&zeroDateTimeBehavior=convertToNull";
			}
			
			// MySQL using PreparedStatement. MUST BE MYSQL5 OR HIGHER
			if (!dbUrl.contains("useServerPrepStmts")) {
				append += "&useServerPrepStmts=true";
			}
			
			if (append.length() > 1)
				props.put("db.url", dbUrl + append);
		}
	}
}
